/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.adapter;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import com.yc.video.config.VideoInfoBean;

import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.ability.TikTok1Ability;
import org.yczbj.ycvideoplayer.utils.ImageLoader;

import java.util.List;
import java.util.Optional;

/**
 * 适配器
 *
 * @since 2021-05-10
 */
public class TikTok1ListAdapter extends BaseItemProvider {
    private static final int DEFAULT_COLUMN = 2;
    private List<VideoInfoBean> data;
    private Context context;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param data 数据
     */
    public TikTok1ListAdapter(Context context, List<VideoInfoBean> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Holder holder;
        if (component == null) {
            component = LayoutScatter.getInstance(componentContainer.getContext()).parse(
                ResourceTable.Layout_item_tiktok_list, null, false);
            holder = new Holder(component);
            component.setTag(holder);
        } else {
            holder = (Holder) component.getTag();
        }
        // init data
        holder.mPosition = position;
        holder.setData();
        return component;
    }

    /**
     * Holder
     *
     * @since 2021-05-12
     */
    public class Holder {
        /**
         * StackLayout
         */
        public StackLayout stackLayout;
        /**
         * 封面图
         */
        public Image mThumb;
        /**
         * 标题
         */
        public Text mTitle;
        /**
         * 角标
         */
        public int mPosition;

        /**
         * 构造函数
         *
         * @param component component
         */
        public Holder(Component component) {
            stackLayout = (StackLayout) component.findComponentById(ResourceTable.Id_stackLayout);
            mTitle = (Text) component.findComponentById(ResourceTable.Id_tv_title);
            mThumb = (Image) component.findComponentById(ResourceTable.Id_iv_thumb);
            component.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    TikTok1Ability.start(component.getContext(), mPosition);
                }
            });
        }

        /**
         * 设置数据
         */
        public void setData() {
            mTitle.setText(data.get(mPosition).getTitle());
            Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
            Point pt = new Point();
            display.get().getSize(pt);
            int width = display.get().getAttributes().width;
            ComponentContainer.LayoutConfig layoutConfig = stackLayout.getLayoutConfig();
            layoutConfig.width = width / DEFAULT_COLUMN;
            stackLayout.setLayoutConfig(layoutConfig); // 除以宽度的一半
            ImageLoader.loadImage(context, data.get(mPosition).getCover(), mThumb);
        }
    }
}
